<template>
  <div class="login">
    <div class="login-box">
      <h3 class="title">{{ title }}</h3>
      <Form class="login-form" ref="form" :model="form" :rules="rule">
        <FormItem prop="user">
          <Input type="text" v-model="form.user" placeholder="请输入用户名">
            <span class="user" slot="prepend"></span>
          </Input>
        </FormItem>
        <FormItem prop="password">
          <Input type="password" v-model="form.password" placeholder="请输入密码">
            <span class="pwd" slot="prepend"></span>
          </Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="handleSubmit('form')">登录</Button>
        </FormItem>
      </Form>
    </div>
  </div>
</template>

<script lang="ts">
import { loginApi } from '@/api'
import { localSave } from '@/libs/util'
import { Component, Vue } from 'vue-property-decorator'

@Component({
  name: 'Login'
})
export default class Login extends Vue {
  form = {
    user: '',
    password: ''
  }
  rule = {
    user: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
  }
  title = '全国中小学生心理健康监测平台'

  async getTitleFn() {
    // const {
    //   status,
    //   data: { value }
    // } = await getAndUpdateTitleApi()
    // if (status === 200) {
    //   this.title = value
    //   document.title = value
    // }
  }

  handleSubmit(this: any, name: string) {
    this.$refs[name].validate(async (valid: any) => {
      if (valid) {
        const { user: account, password } = this.form
        const {
          data: { status, msg ,data}
        } = await loginApi({ account, password })
        if (status === 1) {
          this.$Message.success('登录成功')
          this.$store.commit('setCurrentfixed', data.is_all)
          sessionStorage.setItem('vue_admin_template_token', data.token)
          sessionStorage.setItem('vue_admin_template_permission', JSON.stringify(data))
          this.$store.commit('setPermission_name',data.permission_name)
          this.$store.commit('setMechanism_name',data.mechanism_name)
          if (data.is_all === 1) {
            this.$store.commit('setCurrentArea', 1)
          } else {
            this.$store.commit('setCurrentArea', data.is_all)
            const objs = data.permission
            this.$store.commit('setOnline_id',objs.online_id)
            if (data.is_all === 2) {
              this.$store.commit('setCurrentProvince', objs.province[0])
            } else if (data.is_all === 3) {
              this.$store.commit('setCurrentProvince', objs.province[0])
              this.$store.commit('setCurrentCity', objs.city[0])
            } else if (data.is_all === 4) {
              this.$store.commit('setCurrentProvince', objs.province[0])
              this.$store.commit('setCurrentCity', objs.city[0])
              this.$store.commit('setCountyCode', objs.area[0])
            } else if (data.is_all === 5) {
              this.$store.commit('setCurrentProvince', objs.province[0])
              this.$store.commit('setCurrentCity', objs.city[0])
              this.$store.commit('setCountyCode', objs.area[0])
              this.$store.commit('setSchoolId', objs.school[0])
            }
          } 
          const time = new Date().getTime()
          localSave('time', JSON.stringify(time))
          this.$router.push('/home')
        } else {
          this.$Message.success(msg)
        }
      }
    })
  }
  created() {
    this.getTitleFn()
  }
}
</script>
<style lang="scss" scoped>
.login {
  position: relative;
  height: 100%;
  background: url(../../assets/images/login-bg.jpg) no-repeat center / 100% 100%;

  .login-box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 490px;
    height: 326px;
    padding: 44px 0 50px;
    text-align: center;
    background: url(../../assets/images/login-border.png) no-repeat center / 100% 100%;
    transform: translateX(-50%) translateY(-50%);

    .title {
      margin-bottom: 30px;
      font-size: $font-size-extra-biggest;
      color: $color-primary;
    }

    @media screen and (max-width: 1000px) {
      .title {
        font-size: 24px;
      }
    }
  }
  @media screen and (max-width: 480px) {
    .login-box {
      transform: translateX(-50%) translateY(-50%) scale(0.6);
    }
  }
  @media screen and (max-width: 1000px) {
    .login-box {
      transform: translateX(-50%) translateY(-50%) scale(0.8);
    }
  }
}
</style>
<style lang="scss">
.login-form {
  width: 320px;
  margin: 0 auto;

  .ivu-form-item {
    margin-bottom: 20px;
  }

  .ivu-form-item-content {
    height: 40px;
    line-height: 40px;
  }

  .ivu-input-wrapper {
    height: 40px;
    background: url(../../assets/images/input-border.png) no-repeat center / 100% 100%;

    .ivu-input-group-prepend,
    .ivu-input {
      height: 40px;
      padding-top: 0;
      padding-bottom: 0;
      background-color: transparent;
      border: 0;

      &:focus {
        outline: 0;
        box-shadow: none;
      }
    }

    .ivu-input-group-prepend {
      width: 52px;
      box-sizing: border-box;
    }

    .ivu-input {
      line-height: 40px;
      color: $color-primary-dark;

      &::-webkit-input-placeholder {
        color: $color-primary-dark;
      }

      &::-moz-placeholder {
        color: $color-primary-dark;
      }

      &:-moz-placeholder {
        color: $color-primary-dark;
      }

      &:-ms-input-placeholder {
        color: $color-primary-dark;
      }
    }

    .user,
    .pwd {
      display: inline-block;
      width: 20px;
      height: 100%;
    }

    .user {
      background: url(../../assets/images/name.png) no-repeat center / contain;
    }

    .pwd {
      background: url(../../assets/images/pwd.png) no-repeat center / contain;
    }
  }

  .ivu-form-item-error-tip {
    padding-top: 3px;
    padding-left: 15px;
  }

  .ivu-btn-primary {
    width: 100%;
    height: 40px;
    margin-top: 10px;
    color: $color-primary;
    background: url(../../assets/images/input-border.png) no-repeat center / 100% 100%;
    border: 0;

    &:focus {
      box-shadow: none;
    }
  }
}
</style>
